<?php
require (Yii::app ()->basePath . '/lib/rolling-curl/RollingCurl.php');
/**
 *
 * @author Bidding
 * @see 招投标管理
 */
class BiddingController extends CController {
	/**
	 *
	 * @see 招投标管理-招标列表
	 */
	function actionList() {
		$dicService = new SysDictionaryEntity ();
		// 区域列表
		$category = $dicService->get_list ( - 1, - 1, array (
				'sys_dict_type_id' => 6,
				'deleted' => 0 
		), array (
				"sys_order_num" => 'desc' 
		) );
		$this->render ( 'list', array (
				'category' => $category 
		) );
	}
	
	/**
	 *
	 * @see Ajax 列表
	 */
	function actionAjax_bidding_list() {
		$ret = new stdClass ();
		$ret->status = 401;
		$service = new BiddingEntity ();
		$pg = $_GET ['page']; // 默认刷新页面
		if (empty ( $pg ) || ! is_numeric ( $pg ) || $pg <= 0)
			$pg = 1;
		$where = array (
				'deleted' => 0 
		);
		$p_name = $_GET ['p_name'];
		if ($p_name != "") {
			$where ['p_name'] = "[code] like '%$p_name%' [code]";
		}
		$list = $service->get_list ( $pg, 10, $where, array (
				'id' => 'desc' 
		) );
		$totalCount = $service->get_record_count ( $where );
		$page_count = 1;
		$ret->tct = $totalCount;
		if ($totalCount > 0)
			$page_count = ceil ( $totalCount / 10.0 );
		$ret->pi = $pg;
		if ($list === false) {
			$ret->status = 500;
			$ret->data = "未找到匹配数据";
		} else {
			$ret->status = 200;
			$ret->data = $list;
			$ret->page_count = $page_count;
		}
		echo json_encode ( $ret );
	}
	/**
	 *
	 * @see 招投标管理-招标列表-增加招投标
	 */
	function actionAdd() {
		$dicService = new SysDictionaryEntity ();
		// 区域列表
		$category = $dicService->get_list ( - 1, - 1, array (
				'sys_dict_type_id' => 6,
				'deleted' => 0 
		), array (
				"sys_order_num" => 'desc' 
		) );
		$this->render ( 'add', array (
				'category' => $category 
		) );
	}
	/**
	 *
	 * @see 编辑
	 */
	public function actionEditor() {
		$id = $_GET ['id'];
		$service = new BiddingEntity ();
		$model = $service->get ( $id );
		fb ( $model );
		if (false === $model || is_array ( $model ) === false || count ( $model ) === 0) {
			exit ( "参数错误" );
		}
		$this->render ( 'editor', array (
				'model' => $model 
		) );
	}
	
	/**
	 *
	 * @see ajax提交
	 *
	 */
	public function actionAjax_bidding_add() {
		$ret = new stdClass ();
		$ret->status = 401;
		
		if (Yii::app ()->user->isGuest) {
			$ret->data = "请登录";
			echo json_encode ( $ret );
			Yii::app ()->end ();
		}
		
		$p_name = $_POST ['p_name'];
		// $b_type = $_POST ['b_type'];
		$bidding_types = $_POST ['bidding_types'];
		$repetition = $_POST ['repetition'];
		$organization = $_POST ['organization'];
		$agency = $_POST ['agency'];
		$openaddress = $_POST ['openaddress'];
		// $agent = $_POST ['agent'];
		$remark = $_POST ['remark'];
		$record_time = $_POST ['record_time'];
		$q_time = $_POST ['q_time'];
		$opentime = $_POST ['opentime'];
		$project_manager = $_POST ['project_manager'];
		$biddingprice = $_POST ['biddingprice'];
		$investment = $_POST ['investment'];
		$openunit = $_POST ['openunit'];
		$fldvalue = array (
				'p_name' => $p_name,
				// 'b_type' => $b_type,
				'bidding_types' => $bidding_types,
				'repetition' => $repetition,
				'organization' => $organization,
				'agency' => $agency,
				'openaddress' => $openaddress,
				'remark' => $remark,
				'deleted' => 0,
				'item_number' => date ( 'Ymd', time () ) . randstr ( 4, 'NUMBER' ),
				'q_time' => $q_time,
				'record_time' => $record_time,
				'opentime' => $opentime,
				'pm' => $project_manager,
				'biddingprice' => $biddingprice,
				'investment' => $investment,
				'add_time' => date ( 'Y-m-d H:i:s', time () ),
				'openunit' => $openunit 
		);
		
		$service = new BiddingEntity (); // 定义用户业务数据库操作对象
		$result = $service->insert ( $fldvalue );
		if (is_numeric ( $result ) && $result > 0) {
			$ret->status = 200;
			$ret->data = "操作成功";
		} else {
			$ret->status = 500;
			$ret->data = "操作失败";
		}
		echo json_encode ( $ret );
	}
	/**
	 *
	 * @see ajax更新
	 *
	 */
	public function actionAjax_bidding_update() {
		$ret = new stdClass ();
		$ret->status = 401;
		
		if (Yii::app ()->user->isGuest) {
			$ret->data = "请登录";
			echo json_encode ( $ret );
			Yii::app ()->end ();
		}
		$id = $_POST ['id'];
		$p_name = $_POST ['p_name'];
		// $b_type = $_POST ['b_type'];
		$bidding_types = $_POST ['bidding_types'];
		$repetition = $_POST ['repetition'];
		$organization = $_POST ['organization'];
		$agency = $_POST ['agency'];
		$openaddress = $_POST ['openaddress'];
		// $agent = $_POST ['agent'];
		$remark = $_POST ['remark'];
		$record_time = $_POST ['record_time'];
		$q_time = $_POST ['q_time'];
		$opentime = $_POST ['opentime'];
		$project_manager = $_POST ['project_manager'];
		$biddingprice = $_POST ['biddingprice'];
		$investment = $_POST ['investment'];
		$openunit = $_POST ['openunit'];
		$fldvalue = array (
				'p_name' => $p_name,
				// 'b_type' => $b_type,
				'bidding_types' => $bidding_types,
				'repetition' => $repetition,
				'organization' => $organization,
				'agency' => $agency,
				'openaddress' => $openaddress,
				'remark' => $remark,
				'deleted' => 0,
				'item_number' => date ( 'Ymd', time () ) . randstr ( 4, 'NUMBER' ),
				'q_time' => $q_time,
				'record_time' => $record_time,
				'opentime' => $opentime,
				'pm' => $project_manager,
				'biddingprice' => $biddingprice,
				'investment' => $investment,
				'openunit' => $openunit 
		);
		sys_debug_log ( "编辑招投标" );
		$service = new BiddingEntity ();
		$result = $service->update ( $fldvalue, array (
				'id' => $id 
		) );
		if ($result === true) {
			$ret->status = 200;
			$ret->data = "操作成功";
		} else {
			$ret->status = 500;
			$ret->data = "操作失败";
		}
		echo json_encode ( $ret );
	}
	/**
	 *
	 * @see ajax 删除招投标信息
	 */
	function actionAjax_bidding_delete() {
		$ret = new stdClass ();
		$ret->status = 401;
		
		if (Yii::app ()->user->isGuest) {
			$ret->data = "请登录";
			echo json_encode ( $ret );
			Yii::app ()->end ();
		}
		if (isset ( $_POST ['id'] ) && is_numeric ( $_POST ['id'] )) {
			$id = $_POST ['id'];
			$service = new BiddingEntity ();
			$result = $service->delete_bogus ( $id );
			if ($result) {
				$ret->status = 200;
				$ret->data = "操作成功";
			} else {
				$ret->status = 500;
				$ret->data = "操作失败";
			}
		}
		echo json_encode ( $ret );
	}
	/**
	 *
	 * @see 招投标管理-招标列表-投标情况
	 */
	function actionSituation() {
		$this->render ( 'situation' );
	}
	/**
	 * @招投标详情
	 */
	public function actionDetail() {
		if (isset ( $_GET ['id'] ) && is_numeric ( $_GET ['id'] )) {
			$id = $_GET ['id'];
			$service = new BiddingEntity ();
			$model = $service->get ( $id );
			if (false === $model || is_array ( $model ) === false || count ( $model ) == 0) {
				// 参数错误
				exit ( "参数错误" );
			} else {
				fb ( $model );
				$this->render ( 'detail', array (
						'model' => $model 
				) );
			}
		} else {
			exit ( "参数错误" );
		}
	}
}

	
